///////////////////////////////////////////////////////////////////////////////
//  File:       smartdict.h
//
//  Summary:    Smart pointer classes for Dictionary/Simple list objects
//
//  Sample:     CSF Headlines
//
//---------------------------------------------------------------------
//  This file is part of the Microsoft Commerce Server 2002 SDK
//
//  Copyright (C) 2000 - 2001 Microsoft Corporation.  All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Commerce Server 2002 and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples. 
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////


#pragma once
#ifndef ___SMARTDICT_H__
#define ___SMARTDICT_H__

#include <atlbase.h>

class CSmartDict; //predeclaration

#include "commerce.h"       // IDictionary and ISimplelist definition
#include <crtdbg.h>
#define _Assert(f)          _ASSERTE(f)     // use crtdbg's ASSERT
#include "moneyinfo.h"      // MoneyInfo
#include "mspu_guids.h"

#ifndef HELPER_GETDLLINSTANCE_METHOD
#define HELPER_GETDLLINSTANCE_METHOD _Module.GetResourceInstance()
#endif


//
// The CSmartHelperBase class
// CSmartDict and CSmartList inherit from this class
//

class CSmartHelperBase
{
public:
    CSmartHelperBase()
    {
        *m_wszCompId = '\0';
        *m_wszObjName = '\0';
        m_pMoneyInfo = NULL;
    }

    CSmartHelperBase(CSmartHelperBase &rBase) {  this->CopyBase(rBase); }

    void Init(LPCWSTR pwszObjName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    void Init(LPCWSTR pwszObjName, CMoneyInfo *pMoneyInfo);

    void CopyBase(CSmartHelperBase &rBase);
    void CopyBase(CSmartHelperBase &rBase, LPCWSTR pwszObjName);
    WCHAR *GetName() {return m_wszObjName;}

    // Money Functions that append dict/list names to keys
    HRESULT MultiplyMoney(CURRENCY &result, const CURRENCY &cy, double mul, WCHAR *KeyTryingToCompute=0);
    HRESULT AddMoney(CURRENCY &result, const CURRENCY &cy1, const CURRENCY &cy2, WCHAR *KeyTryingToCompute=0);
    HRESULT SubtractMoney(CURRENCY &result, const CURRENCY &cy1, const CURRENCY &cy2, WCHAR *KeyTryingToCompute=0);

    // Error handling methods
    void    ErrMissingValue(BSTR bstrName, VARTYPE vt);
    void    ErrMissingValue(BSTR bstrName);
    void    ErrValueWrongType(BSTR bstrName, VARTYPE vtExpected, VARTYPE vtGot);
    void    ErrNoInterface(BSTR bstrName, LPCWSTR pwszInterface);
    void    ErrNoInterface(long index, LPCWSTR pwszInterface);
    void    ErrValueWrongType(long index, VARTYPE vtExpected, VARTYPE vtGot);
    void    ErrMismatchedMoney(BSTR bstrName, BSTR bstrLegacyName);

protected:

    CComPtr<IDispatch>      m_pdispContext;
    WCHAR                   m_wszCompId[50];
    WCHAR                   m_wszObjName[50];
    CMoneyInfo*             m_pMoneyInfo;

    // no implementation
    CSmartHelperBase &operator=(CSmartHelperBase &);
};

class CSmartList : public CSmartHelperBase
{
public:
    // Default constructor is a no-op.  Object must be initialized with the Init() method before use.
    CSmartList() : CSmartHelperBase() {}

    // Copy constructor - increases the ref count on the simplelist being wrapped (use Clone to copy)
    CSmartList(CSmartList &rBase) { Init(rBase); }

    // Initializing constructors
    CSmartList(ISimpleList *plist, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL)
    {
        this->Init(plist, pwszListName, pwszCompId, pdispContext);
    }
    CSmartList(ISimpleList *plist, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo)
    {
        this->Init(plist, pwszListName, pMoneyInfo);
    }
    CSmartList(ISimpleList *plist, LPCWSTR pwszListName, CSmartHelperBase &rBase)
    {
        this->Init(plist, pwszListName, rBase);
    }

    // The Init() methods initialize the List helper object.  Once a ListHelper object
    // has been initialized, it can be used just like an ISimpleList pointer.
    //
    // Params:
    //      plist                   - pointer to ISimpleList
    //      pdisp                   - pointer to IDispatch (QI'ed for ISimpleList)
    //      pvarlist                - pointer to Variant holding IDispatch (QI'ed for ISimpleList)
    //                                an IDictionary interface.
    //      pwszListName            - simplelist name, used in error messages
    //      pwszCompId              - the component ID (ProgId) of the caller, used in error messages
    //      pMoneyInfo              - pointer to a MoneyInfo class, that describes currency rounding/mode info
    //      rBase                   - Dictionary or List helper object to copy component name, and dispContext from
    // Returns:
    //      E_NOINTERFACE           - the IDispatch or VARIANT argument didn't contain a reference to ISimpleList
    //      E_INVALIDARG            - one of the required parameters was NULL
    //
    void Init(ISimpleList *plist, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    void Init(ISimpleList *plist, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo);
    void Init(ISimpleList *plist, LPCWSTR pwszListName, CSmartHelperBase &rBase);
    void Init(CSmartList &rList);
    HRESULT Init(IDispatch *pdisp, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    HRESULT Init(IDispatch *pdisp, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo);
    HRESULT Init(IDispatch *pdisp, LPCWSTR pwszListName, CSmartHelperBase &rBase);
    HRESULT Init(VARIANT *pvarlist, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    HRESULT Init(VARIANT *pvarlist, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo);
    HRESULT Init(VARIANT *pvarlist, LPCWSTR pwszListName, CSmartHelperBase &rBase);

    // Creates a new SimpleList to wrap
    HRESULT CreateNewList(LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    HRESULT CreateNewList(LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo);
    HRESULT CreateNewList(LPCWSTR pwszListName, CSmartHelperBase &rBase);

    // SetList() methods set the dictionary.  Init() must be called first or these return OLE_E_BLANK
    //
    void SetList(ISimpleList *plist);
    HRESULT SetList(IDispatch *pdisp);
    HRESULT SetList(VARIANT *pvarlist);

    // Attach() attaches the wrapper to an ISimpleList but doesn't AddRef it
    void Attach(ISimpleList *plist);

    // Detach() returns the wrapped interface but doesn't Release it
    ISimpleList* Detach();

    // vt is the variant type that should be returned.  If bStrictType is FALSE then ListHelper will
    // attempt to coerce any other type to vt.
    HRESULT GetItem(long index, VARIANT *pvarValue, VARTYPE vt=VT_EMPTY, BOOL bStrictType=FALSE);
    HRESULT GetItem(long index, BSTR *pbstrValue);
    HRESULT GetItem(long index, int *pintValue);
    HRESULT GetItem(long index, long *plongValue);
    HRESULT GetItem(long index, short *pshortValue);
    HRESULT GetItem(long index, float *pfloatValue);
    HRESULT GetItem(long index, double *pdoubleValue);
    HRESULT GetItem(long index, IDispatch **ppdispValue);
    HRESULT GetItem(long index, IUnknown **ppunkValue);
    HRESULT GetItem(long index, IDictionary **ppdictValue);
    HRESULT GetItem(long index, ISimpleList **pplistValue);

    // index of -1 means to add instead of insert
    HRESULT PutItem(long index, VARIANT *pvarValue);
    HRESULT PutrefItem(long index, VARIANT *pvarValue);
    HRESULT PutItem(long index, BSTR bstrValue);
    HRESULT PutItem(long index, int intValue);
    HRESULT PutItem(long index, long longValue);
    HRESULT PutItem(long index, short shortValue);
    HRESULT PutItem(long index, float floatValue);
    HRESULT PutItem(long index, double doubleValue);
    HRESULT PutItem(long index, IDispatch *pdispValue);
    HRESULT PutItem(long index, IUnknown *punkValue);
    HRESULT PutItem(long index, IDictionary *pdictValue);
    HRESULT PutItem(long index, ISimpleList *plistValue);

    // add item to end of list
    HRESULT Add(VARIANT *pvarValue)         { return this->PutItem(-1, pvarValue); }
    HRESULT Add(BSTR bstrValue)             { return this->PutItem(-1, bstrValue); }
    HRESULT Add(int intValue)               { return this->PutItem(-1, intValue); }
    HRESULT Add(long longValue)             { return this->PutItem(-1, longValue); }
    HRESULT Add(short shortValue)           { return this->PutItem(-1, shortValue); }
    HRESULT Add(float floatValue)           { return this->PutItem(-1, floatValue); }
    HRESULT Add(double doubleValue)         { return this->PutItem(-1, doubleValue); }
    HRESULT Add(IDispatch *pdispValue)      { return this->PutItem(-1, pdispValue); }
    HRESULT Add(IUnknown *punkValue)        { return this->PutItem(-1, punkValue); }
    HRESULT Add(IDictionary *pdictValue)    { return this->PutItem(-1, pdictValue); }
    HRESULT Add(ISimpleList *plistValue)    { return this->PutItem(-1, plistValue); }

    // delete
    HRESULT Delete(long index);

    // ICloneable
    // return a deep copy of the list wrapped by *this
    HRESULT Clone(ISimpleList **pplistClone);
    // initialize rClone to be a deep copy of *this
    HRESULT Clone(CSmartList &rClone);

    // get count
    HRESULT GetCount(long *pl);

    // GetNewEnum()/GetNextItem() - These methods encapsulate IEnumVariant for iterating through
    // the entries in a List
    //
    // First call GetNewEnum to start an enumeration of the keys.  Then call GetNextItem until it
    // returns FALSE:
    //
    //      HRESULT hr = S_OK;
    //      CComVariant var;
    //      hr = list.GetNewEnum();
    //      if (FAILED(hr))
    //          // handle enumeration failure
    //      while (list.GetNextItem(&var, &hr))
    //      {
    //          // do work using var
    //          // ...
    //          var.Clear();
    //      }
    //      if (FAILED(hr))
    //          // handle enumeration failure
    //
    // Note that if there is an error in GetNextItem, FALSE will be the return value and
    // *phr will contain the HRESULT value for the error.
    //
    HRESULT GetNewEnum(VARTYPE vt=VT_EMPTY, BOOL bStrictType=FALSE);
    BOOL    GetNextItem(VARIANT *pvar, HRESULT *phr);
    BOOL    GetNextItem(CSmartDict &rdict, HRESULT *phr);

    // Overloaded operators allow a CSmartList object to be used like an ISimpleList *
    ISimpleList **operator&() { _Assert(m_plist==NULL); return &m_plist; }
    ISimpleList *operator->() { _Assert(m_plist); return m_plist; }
    operator ISimpleList*() { return m_plist; }
    BOOL operator!() { return m_plist==NULL; }

    // operator =
    CSmartList &operator=(CSmartList &rList)
    {
        this->Init(rList);
        return *this;
    }

    // enumeration that tells GetItem whether a property is Optional or Required (bOptional param)
    enum { Required=FALSE, Optional=TRUE };

    // enumeration that tells GetItem whether or not to attempt VariantChangeType (bStrictType)
    enum { NoStrictType=FALSE, StrictType=TRUE };

    CComPtr<ISimpleList>    m_plist;

protected:
    CComPtr<IEnumVARIANT>   m_penumVariant;
    int                     m_enumPosition;
    VARTYPE                 m_enumVarType;

    // create a new SimpleList object to wrap
    HRESULT Create();

};



class CSmartDict: public CSmartHelperBase
{
public:
    // Default constructor is a no-op.  Object must be initialized with the Init() method before use.
    CSmartDict() : CSmartHelperBase() {}

    // Copy constructor
    CSmartDict(CSmartDict &rBase) { this->Init(rBase); }

    // Initializing constructors
    CSmartDict(IDictionary *pdict, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL)
    {
        this->Init(pdict, pwszDictName, pwszCompId, pdispContext);
    }
    CSmartDict(IDictionary *pdict, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo)
    {
        this->Init(pdict, pwszDictName, pMoneyInfo);
    }
    CSmartDict(IDictionary *pdict, LPCWSTR pwszDictName, CSmartHelperBase &rBase)
    {
        this->Init(pdict, pwszDictName, rBase);
    }

    // The Init() methods initialize the Dictionary helper object.  Once a DictionaryHelper object
    // has been initialized, it can be used just like an IDictionary pointer.
    //
    // Params:
    //      pdict                   - pointer to IDictionary
    //      pdisp                   - pointer to IDispatch (QI'ed for IDictionary)
    //      pvardict                - pointer to Variant holding IDispatch (QI'ed for IDictionary)
    //                                an IDictionary interface.
    //      pwszDictName            - dictionary name, used in error messages
    //      pwszCompId              - the component ID (ProgId) of the caller, used in error messages
    //      pMoneyInfo              - pointer to a MoneyInfo class, that describes currency rounding/mode info
    //      rBase                   - Dictionary or List helper object to copy ProgId and dispContext from
    // Returns:
    //      E_NOINTERFACE           - the IDispatch or VARIANT argument didn't contain a reference to IDictionary
    //      E_INVALIDARG            - one of the required parameters was NULL
    //
    void Init(IDictionary *pdict, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    void Init(IDictionary *pdict, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo);
    void Init(IDictionary *pdict, LPCWSTR pwszDictName, CSmartHelperBase &rBase);
    void Init(CSmartDict &rBase);
    HRESULT Init(IDispatch *pdisp, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    HRESULT Init(IDispatch *pdisp, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo);
    HRESULT Init(IDispatch *pdisp, LPCWSTR pwszDictName, CSmartHelperBase &rBase);
    HRESULT Init(VARIANT *pvardict, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    HRESULT Init(VARIANT *pvardict, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo);
    HRESULT Init(VARIANT *pvardict, LPCWSTR pwszDictName, CSmartHelperBase &rBase);

    // Creates a new dictionary to wrap
    HRESULT CreateNewDict(LPCWSTR pwszDictName, CSmartHelperBase &rBase);
    HRESULT CreateNewDict(LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext=NULL);
    HRESULT CreateNewDict(LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo);

    // SetDict() methods set the dictionary.  Init() must be called first or these return OLE_E_BLANK
    //
    void SetDict(IDictionary *pdict);
    HRESULT SetDict(IDispatch *pdisp);
    HRESULT SetDict(VARIANT *pvardict);

    // Attach() attaches the wrapper to an IDictionary but doesn't AddRef it
    void Attach(IDictionary *pdict);

    // Detach() returns the wrapped interface but doesn't Release it
    IDictionary* Detach();


    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    // GetValue/PutValue
    //
    // These methods are more efficient if you already have the dictionary keys allocated as BSTRs.
    // Use these methods to prevent allocating an additional BSTR for the dictionary key
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////


    // The first GetValue() method is the most flexible and returns a VARIANT type.
    //
    // Params:
    //      bstrName                - the key of the dictionary entry to look up
    //      pvarValue               - pointer to VARIANT to receive the result
    //      vt                      - (optional) the VARTYPE that should be returned (VT_EMPTY means any type)
    //      bOptional               - (optional) specifies whether it should be an error (FALSE) if there is
    //                                no value associated with the key.  Default is not optional (false).
    //      bStrictType             - (optional) if the VARTYPE of the value doesn't match the requested VARTYPE, then if
    //                                bStrictType is TRUE an error will be reported, else a conversion using
    //                                the VariantChangeType API will be attempted.  Default is to attempt conversion (FALSE).
    //
    HRESULT GetValue(LPCWSTR wszName, VARIANT *pvarValue, VARTYPE vt=VT_EMPTY, BOOL bOptional=FALSE, BOOL bStrictType=FALSE);

    // The remaining GetValue() methods return a value of a specific type.  If the value is not of the required type,
    // then the VariantChangeType API is used to attempt to convert the variant to the correct type.  Each of these
    // methods calls the GetValue(VARIANT) method above to actually get the value.
    //
    // The pbFound parameter can be used if bOptional is set to TRUE as a way to find out if the value
    // was actually present in the dictionary.
    //
    HRESULT GetValue(LPCWSTR wszName, BSTR *pbstrValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, int *pintValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, long *plongValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, short *pshortValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, float *pfloatValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, double *pdoubleValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, LPCWSTR wszLegacyName, CURRENCY *pcyValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, IDispatch **ppdispValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, IUnknown **ppunkValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, IDictionary **ppdictValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, ISimpleList **pplistValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, CSmartDict &rdict, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValue(LPCWSTR wszName, CSmartList &rlist, BOOL bOptional=FALSE, BOOL *pbFound=NULL);

    // These methods call GetValue and if the value wasn't found, they set the return value to the default.
    HRESULT GetValueDefault(LPCWSTR wszName, LPCWSTR pwszDefault, BSTR *pbstrValue);
    HRESULT GetValueDefault(LPCWSTR wszName, int nDefault, int *pintValue);
    HRESULT GetValueDefault(LPCWSTR wszName, long nDefault, long *plongValue);
    HRESULT GetValueDefault(LPCWSTR wszName, short nDefault, short *pshortValue);
    HRESULT GetValueDefault(LPCWSTR wszName, float flDefault, float *pfloatValue);
    HRESULT GetValueDefault(LPCWSTR wszName, double dblDefault, double *pdoubleValue);
    HRESULT GetValueDefault(LPCWSTR wszName, LPCWSTR wszLegacyName, CURRENCY cyDefault, CURRENCY *pcyValue);

    // The PutValue() methods add or overwrite an entry in the dictionary.  Note that if any of the
    // IDispatch, IDictionary, or ISimpleList methods are used, it automatically uses PutrefValue
    // instead of PutValue (which tries to get a default value from the interface).
    //
    HRESULT PutValue(LPCWSTR wszName, VARIANT *pvarValue);
    HRESULT PutrefValue(LPCWSTR wszName, VARIANT *pvarValue);
    HRESULT PutValue(LPCWSTR wszName, BSTR bstrValue);
    HRESULT PutValue(LPCWSTR wszName, int intValue);
    HRESULT PutValue(LPCWSTR wszName, long longValue);
    HRESULT PutValue(LPCWSTR wszName, short shortValue);
    HRESULT PutValue(LPCWSTR wszName, float floatValue);
    HRESULT PutValue(LPCWSTR wszName, double doubleValue);
    HRESULT PutValue(LPCWSTR wszName, LPCWSTR wszLegacyName, CURRENCY cyValue);
    HRESULT PutValue(LPCWSTR wszName, IDispatch *pdispValue);
    HRESULT PutValue(LPCWSTR wszName, IUnknown *punkValue);
    HRESULT PutValue(LPCWSTR wszName, IDictionary *pdictValue);
    HRESULT PutValue(LPCWSTR wszName, ISimpleList *plistValue);


    ///////////////////////////////////////////////////////////////////////////////////////////////
    //
    // GetValueBSTR/PutValueBSTR
    //
    // These methods are more efficient if you already have the dictionary keys allocated as BSTRs.
    // Use these methods to avoid an uneccessary SysAllocString
    //
    ///////////////////////////////////////////////////////////////////////////////////////////////
    HRESULT GetValueBSTR(BSTR bstrName, VARIANT *pvarValue, VARTYPE vt=VT_EMPTY, BOOL bOptional=FALSE, BOOL bStrictType=FALSE);

    HRESULT GetValueBSTR(BSTR bstrName, BSTR *pbstrValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, int *pintValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, long *plongValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, short *pshortValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, float *pfloatValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, double *pdoubleValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, BSTR bstrLegacyName, CURRENCY *pcyValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, IDispatch **ppdispValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, IUnknown **ppunkValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, IDictionary **ppdictValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, ISimpleList **pplistValue, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, CSmartDict &rdict, BOOL bOptional=FALSE, BOOL *pbFound=NULL);
    HRESULT GetValueBSTR(BSTR bstrName, CSmartList &rlist, BOOL bOptional=FALSE, BOOL *pbFound=NULL);

    HRESULT GetValueDefaultBSTR(BSTR bstrName, LPCWSTR pwszDefault, BSTR *pbstrValue);
    HRESULT GetValueDefaultBSTR(BSTR bstrName, int nDefault, int *pintValue);
    HRESULT GetValueDefaultBSTR(BSTR bstrName, long nDefault, long *plongValue);
    HRESULT GetValueDefaultBSTR(BSTR bstrName, short nDefault, short *pshortValue);
    HRESULT GetValueDefaultBSTR(BSTR bstrName, float flDefault, float *pfloatValue);
    HRESULT GetValueDefaultBSTR(BSTR bstrName, double dblDefault, double *pdoubleValue);
    HRESULT GetValueDefaultBSTR(BSTR bstrName, BSTR bstrLegacyName, CURRENCY cyDefault, CURRENCY *pcyValue);

    HRESULT PutValueBSTR(BSTR bstrName, VARIANT *pvarValue);
    HRESULT PutrefValueBSTR(BSTR bstrName, VARIANT *pvarValue);
    HRESULT PutValueBSTR(BSTR bstrName, BSTR bstrValue);
    HRESULT PutValueBSTR(BSTR bstrName, int intValue);
    HRESULT PutValueBSTR(BSTR bstrName, long intValue);
    HRESULT PutValueBSTR(BSTR bstrName, short shortValue);
    HRESULT PutValueBSTR(BSTR bstrName, float floatValue);
    HRESULT PutValueBSTR(BSTR bstrName, double doubleValue);
    HRESULT PutValueBSTR(BSTR bstrName, BSTR bstrLegacyName, CURRENCY cyValue);
    HRESULT PutValueBSTR(BSTR bstrName, IDispatch *pdispValue);
    HRESULT PutValueBSTR(BSTR bstrName, IUnknown *punkValue);
    HRESULT PutValueBSTR(BSTR bstrName, IDictionary *pdictValue);
    HRESULT PutValueBSTR(BSTR bstrName, ISimpleList *plistValue);

    // ICloneable
    HRESULT Clone(CSmartDict &rClone);
    HRESULT Clone(IDictionary **ppdictClone);

    // get count
    HRESULT GetCount(long *pl);

    // GetNewEnum()/GetNextKey() - These methods encapsulate IEnumVariant for iterating through
    // the entries in a dictionary.
    //
    // First call GetNewEnum to start an enumeration of the keys.  Then call GetNextKey until it
    // returns FALSE:
    //
    //      HRESULT hr = S_OK;
    //      CComBSTR bstrKey;
    //      hr = dict.GetNewEnum();
    //      if (FAILED(hr))
    //          // handle enumeration failure
    //      while (dict.GetNextKey(&bstrKey, &hr))
    //      {
    //          // do work using bstrKey
    //          // ...
    //          bstrKey.Empty();
    //      }
    //      if (FAILED(hr))
    //          // handle enumeration failure
    //
    // Note that if there is an error in GetNextKey, FALSE will be the return value and
    // *phr will contain the HRESULT value for the error.
    //
    HRESULT GetNewEnum();
    BOOL    GetNextKey(BSTR *pbstr, HRESULT *phr=NULL);

    // Overloaded operators allow a CSmartDict object to be used like an IDictionary *
    IDictionary **operator&() { _Assert(m_pdict==NULL); return &m_pdict; }
    IDictionary *operator->() { _Assert(m_pdict); return m_pdict; }
    operator IDictionary*() { return m_pdict; }
    BOOL operator!() { return m_pdict==NULL; }

    // operator =
    CSmartDict &operator=(CSmartDict &rDict)
    {
        this->Init(rDict);
        return *this;
    }

    // enumeration that tells GetValue whether a property is Optional or Required (bOptional param)
    enum { Required=FALSE, Optional=TRUE };

    // enumeration that tells GetValue whether or not to attempt VariantChangeType (bStrictType)
    enum { NoStrictType=FALSE, StrictType=TRUE };

    CComPtr<IDictionary>    m_pdict;

protected:
    CComPtr<IEnumVARIANT>   m_penumVariant;

    // create a new Dictionary to wrap
    HRESULT Create();

};



//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//*****************************************************************************************
//=========================================================================================
//
//  CSmartDict inlines
//
//=========================================================================================
//*****************************************************************************************
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


inline void CSmartDict::Init(IDictionary *pdict, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszDictName, pwszCompId, pdispContext);
    this->SetDict(pdict);
}

inline HRESULT CSmartDict::Init(IDispatch *pdisp, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszDictName, pwszCompId, pdispContext);
    return this->SetDict(pdisp);
}

inline HRESULT CSmartDict::Init(VARIANT *pvardict, LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszDictName, pwszCompId, pdispContext);
    return this->SetDict(pvardict);
}

inline void CSmartDict::Init(IDictionary *pdict, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszDictName, pMoneyInfo);
    this->SetDict(pdict);
}

inline HRESULT CSmartDict::Init(IDispatch *pdisp, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszDictName, pMoneyInfo);
    return this->SetDict(pdisp);
}

inline HRESULT CSmartDict::Init(VARIANT *pvardict, LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszDictName, pMoneyInfo);
    return this->SetDict(pvardict);
}

inline void CSmartDict::Init(IDictionary *pdict, LPCWSTR pwszDictName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszDictName);
    this->SetDict(pdict);
}

inline HRESULT CSmartDict::Init(IDispatch *pdisp, LPCWSTR pwszDictName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszDictName);
    return this->SetDict(pdisp);
}

inline HRESULT CSmartDict::Init(VARIANT *pvardict, LPCWSTR pwszDictName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszDictName);
    return this->SetDict(pvardict);
}

inline void CSmartDict::Init(CSmartDict &rBase)
{
    CSmartHelperBase::CopyBase(rBase);
    m_pdict = rBase.m_pdict;
}

inline HRESULT CSmartDict::CreateNewDict(LPCWSTR pwszDictName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszDictName);
    return this->Create();
}

inline HRESULT CSmartDict::CreateNewDict(LPCWSTR pwszDictName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszDictName, pwszCompId, pdispContext);
    return this->Create();
}

inline HRESULT CSmartDict::CreateNewDict(LPCWSTR pwszDictName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszDictName, pMoneyInfo);
    return this->Create();
}

inline void CSmartDict::SetDict(IDictionary *pdict)
{
    if (m_pdict) m_pdict.Release();
    m_pdict = pdict;
}

inline void CSmartDict::Attach(IDictionary *pdict)
{
    if (m_pdict) m_pdict.p->Release();
    m_pdict.p = pdict;
}

inline IDictionary* CSmartDict::Detach()
{
    IDictionary *pdictTemp = m_pdict;
    m_pdict.p = NULL;
    return pdictTemp;
}

inline HRESULT CSmartDict::SetDict(IDispatch *pdisp)
{
    HRESULT hr = S_OK;
    IDictionary* pdict = NULL;
    if (NULL!=pdisp)
    {
        if (FAILED(hr = pdisp->QueryInterface(IID_IDictionary, (void **)&pdict)))
            return hr;
    }
    this->Attach(pdict);
    return hr;
}

inline HRESULT CSmartDict::SetDict(VARIANT *pvardict)
{
    if ((V_VT(pvardict)&VT_TYPEMASK) != VT_DISPATCH)
        return E_NOINTERFACE;
    if (V_ISBYREF(pvardict))
        return this->SetDict(*(V_DISPATCHREF(pvardict)));
    else
        return this->SetDict(V_DISPATCH(pvardict));
}


inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, LPCWSTR pwszDefault, BSTR *pbstrValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, pbstrValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        if (!(*pbstrValue = SysAllocString(pwszDefault)))
        {
            hr = E_OUTOFMEMORY;
            goto CleanUp;
        }
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, int nDefault, int *pintValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, pintValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        *pintValue = nDefault;
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, long nDefault, long *plongValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, plongValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        *plongValue = nDefault;
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, short nDefault, short *pshortValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, pshortValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        *pshortValue = nDefault;
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, float flDefault, float *pfloatValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, pfloatValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        *pfloatValue = flDefault;
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, double dblDefault, double *pdoubleValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, pdoubleValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        *pdoubleValue = dblDefault;
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::GetValueDefaultBSTR(BSTR bstrName, BSTR bstrLegacyName, CURRENCY cyDefault, CURRENCY *pcyValue)
{
    BOOL    bFound;
    HRESULT hr = S_OK;

    if (FAILED(hr = this->GetValueBSTR(bstrName, bstrLegacyName, pcyValue, Optional, &bFound)))
        goto CleanUp;
    if (!bFound)
        *pcyValue = cyDefault;
CleanUp:
    return hr;
}


inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, VARIANT *pvarValue)
{
    _Assert(m_pdict);
    _Assert(bstrName);
    _Assert(pvarValue);
    if (!m_pdict)
        return OLE_E_BLANK;
    if (!bstrName || !pvarValue)
        return E_INVALIDARG;

    return m_pdict->put_Value(bstrName, *pvarValue);
}

inline HRESULT CSmartDict::PutrefValueBSTR(BSTR bstrName, VARIANT *pvarValue)
{
    _Assert(m_pdict);
    _Assert(bstrName);
    _Assert(pvarValue);
    if (!m_pdict)
        return OLE_E_BLANK;
    if (!bstrName || !pvarValue)
        return E_INVALIDARG;

    return m_pdict->putref_Value(bstrName, *pvarValue);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, BSTR bstrValue)
{
    _Assert(bstrValue);
    VARIANT var[1];
    V_VT(var) = VT_BSTR;
    V_BSTR(var) = bstrValue;
    return this->PutValueBSTR(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, int intValue)
{
    VARIANT var[1];
    V_VT(var) = VT_I4;
    V_I4(var) = intValue;
    return this->PutValueBSTR(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, long longValue)
{
    VARIANT var[1];
    V_VT(var) = VT_I4;
    V_I4(var) = longValue;
    return this->PutValueBSTR(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, short shortValue)
{
    VARIANT var[1];
    V_VT(var) = VT_I2;
    V_I2(var) = shortValue;
    return this->PutValueBSTR(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, float floatValue)
{
    VARIANT var[1];
    V_VT(var) = VT_R4;
    V_R4(var) = floatValue;
    return this->PutValueBSTR(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, double doubleValue)
{
    VARIANT var[1];
    V_VT(var) = VT_R8;
    V_R8(var) = doubleValue;
    return this->PutValueBSTR(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, BSTR bstrLegacyName, CURRENCY cyValue)
{
    _Assert(m_pMoneyInfo);
    HRESULT hr = S_OK;

    VARIANT var[1];
    V_VT(var) = VT_CY;
    V_CY(var) = cyValue;
    int pennies;

    // if in 3.0 compatibility mode, write an int to the legacy key...
    if (3 == m_pMoneyInfo->GetMode())
    {
        if (FAILED(hr = m_pMoneyInfo->CurrencyToInt(cyValue, pennies, bstrLegacyName)))
            goto CleanUp;
        if (FAILED(hr = this->PutValueBSTR(bstrLegacyName, pennies)))
            goto CleanUp;
    }

    // Round the currency before writing it...
    if (FAILED(hr = m_pMoneyInfo->RoundCurrency(V_CY(var), V_CY(var))))
        goto CleanUp;
    if (FAILED(hr = this->PutValueBSTR(bstrName, var)))
        goto CleanUp;
CleanUp:
    return hr;
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, IDispatch *pdispValue)
{
    _Assert(pdispValue);
    if (!pdispValue)
        return E_INVALIDARG;

    VARIANT var[1];
    V_VT(var) = VT_DISPATCH;
    V_DISPATCH(var) = pdispValue;
    return this->PutrefValue(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, IUnknown *punkValue)
{
    _Assert(punkValue);
    if (!punkValue)
        return E_INVALIDARG;

    VARIANT var[1];
    V_VT(var) = VT_UNKNOWN;
    V_UNKNOWN(var) = punkValue;
    return this->PutrefValue(bstrName, var);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, IDictionary *pdictValue)
{
    _Assert(pdictValue);
    if (!pdictValue)
        return E_INVALIDARG;

    HRESULT hr = S_OK;
    CComPtr<IDispatch> pdisp;
    if (FAILED(hr = pdictValue->QueryInterface(IID_IDispatch, (void **)&pdisp)))
        return hr;
    return this->PutValueBSTR(bstrName, pdisp);
}

inline HRESULT CSmartDict::PutValueBSTR(BSTR bstrName, ISimpleList *plistValue)
{
    _Assert(plistValue);
    if (!plistValue)
        return E_INVALIDARG;

    HRESULT hr = S_OK;
    CComPtr<IDispatch> pdisp;
    if (FAILED(hr = plistValue->QueryInterface(IID_IDispatch, (void **)&pdisp)))
        return hr;
    return this->PutValueBSTR(bstrName, pdisp);
}

inline HRESULT CSmartDict::GetCount(long *pl)
{
    if (!pl)
        return E_INVALIDARG;
    if (!m_pdict)
        return OLE_E_BLANK;
    return m_pdict->get_Count(pl);
}

/////////////////////////////////////////////////////////////////////////////////
//
// CSmartDict
// Wide character string inlines
//
/////////////////////////////////////////////////////////////////////////////////

// Define a Macro to make this code more compact
#define WSZNAME2BSTRNAME            \
    if (NULL==wszName)              \
        return E_INVALIDARG;        \
    CComBSTR bstrName = wszName;    \
    if (!bstrName)             \
        return E_OUTOFMEMORY;

// Define a Macro to make this code more compact
#define WSZLEGACYNAME2BSTRLEGACYNAME                    \
    CComBSTR bstrLegacyName;                            \
    if (m_pMoneyInfo && 3 == m_pMoneyInfo->GetMode() )  \
    {                                                   \
        if (NULL==wszLegacyName)                        \
            return E_INVALIDARG;                        \
        bstrLegacyName = wszLegacyName;                 \
        if (!bstrLegacyName)                            \
            return E_OUTOFMEMORY;                       \
    }

//
// GetValue
//

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, VARIANT *pvarValue, VARTYPE vt, BOOL bOptional, BOOL bStrictType)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pvarValue, vt, bOptional);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, BSTR *pbstrValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pbstrValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, int *pintValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pintValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, long *plongValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, plongValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, short *pshortValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pshortValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, float *pfloatValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pfloatValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, double *pdoubleValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pdoubleValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, LPCWSTR wszLegacyName, CURRENCY *pcyValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    WSZLEGACYNAME2BSTRLEGACYNAME
    return this->GetValueBSTR(bstrName, bstrLegacyName, pcyValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, IDispatch **ppdispValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, ppdispValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, IUnknown **ppunkValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, ppunkValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, IDictionary **ppdictValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, ppdictValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, ISimpleList **pplistValue, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, pplistValue, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, CSmartDict &rdict, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, rdict, bOptional, pbFound);
}

inline HRESULT CSmartDict::GetValue(LPCWSTR wszName, CSmartList &rlist, BOOL bOptional, BOOL *pbFound)
{
    WSZNAME2BSTRNAME
    return this->GetValueBSTR(bstrName, rlist, bOptional, pbFound);
}


//
// GetValueDefault
//

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, LPCWSTR pwszDefault, BSTR *pbstrValue)
{
    WSZNAME2BSTRNAME
    return this->GetValueDefaultBSTR(bstrName, pwszDefault, pbstrValue);
}

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, int nDefault, int *pintValue)
{
    WSZNAME2BSTRNAME
    return this->GetValueDefaultBSTR(bstrName, nDefault, pintValue);
}

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, long nDefault, long *plongValue)
{
    WSZNAME2BSTRNAME
    return this->GetValueDefaultBSTR(bstrName, nDefault, plongValue);
}

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, short nDefault, short *pshortValue)
{
    WSZNAME2BSTRNAME
    return this->GetValueDefaultBSTR(bstrName, nDefault, pshortValue);
}

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, float flDefault, float *pfloatValue)
{
    WSZNAME2BSTRNAME
    return this->GetValueDefaultBSTR(bstrName, flDefault, pfloatValue);
}

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, double dblDefault, double *pdoubleValue)
{
    WSZNAME2BSTRNAME
    return this->GetValueDefaultBSTR(bstrName, dblDefault, pdoubleValue);
}

inline HRESULT CSmartDict::GetValueDefault(LPCWSTR wszName, LPCWSTR wszLegacyName, CURRENCY cyDefault, CURRENCY *pcyValue)
{
    WSZNAME2BSTRNAME
    WSZLEGACYNAME2BSTRLEGACYNAME
    return this->GetValueDefaultBSTR(bstrName, bstrLegacyName, cyDefault, pcyValue);
}


//
// PutValue
//

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, VARIANT *pvarValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, pvarValue);
}

inline HRESULT CSmartDict::PutrefValue(LPCWSTR wszName, VARIANT *pvarValue)
{
    WSZNAME2BSTRNAME
    return this->PutrefValueBSTR(bstrName, pvarValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, BSTR bstrValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, bstrValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, int intValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, intValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, long longValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, longValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, short shortValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, shortValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, float floatValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, floatValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, double doubleValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, doubleValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, LPCWSTR wszLegacyName, CURRENCY cyValue)
{
    WSZNAME2BSTRNAME
    WSZLEGACYNAME2BSTRLEGACYNAME
    return this->PutValueBSTR(bstrName, bstrLegacyName, cyValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, IDispatch *pdispValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, pdispValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, IUnknown *punkValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, punkValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, IDictionary *pdictValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, pdictValue);
}

inline HRESULT CSmartDict::PutValue(LPCWSTR wszName, ISimpleList *plistValue)
{
    WSZNAME2BSTRNAME
    return this->PutValueBSTR(bstrName, plistValue);
}





//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//*****************************************************************************************
//=========================================================================================
//
//  CSmartList inlines
//
//=========================================================================================
//*****************************************************************************************
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

inline void CSmartList::Init(ISimpleList *plist, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszListName, pwszCompId, pdispContext);
    this->SetList(plist);
}

inline HRESULT CSmartList::Init(VARIANT *pvarlist, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszListName, pwszCompId, pdispContext);
    return this->SetList(pvarlist);
}

inline HRESULT CSmartList::Init(IDispatch *pdisp, LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszListName, pwszCompId, pdispContext);
    return this->SetList(pdisp);
}

inline void CSmartList::Init(ISimpleList *plist, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszListName, pMoneyInfo);
    this->SetList(plist);
}

inline HRESULT CSmartList::Init(VARIANT *pvarlist, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszListName, pMoneyInfo);
    return this->SetList(pvarlist);
}

inline HRESULT CSmartList::Init(IDispatch *pdisp, LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszListName, pMoneyInfo);
    return this->SetList(pdisp);
}


inline void CSmartList::Init(ISimpleList *plist, LPCWSTR pwszListName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszListName);
    this->SetList(plist);
}

inline HRESULT CSmartList::Init(IDispatch *pdisp, LPCWSTR pwszListName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszListName);
    return this->SetList(pdisp);
}

inline HRESULT CSmartList::Init(VARIANT *pvarlist, LPCWSTR pwszListName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszListName);
    return this->SetList(pvarlist);
}

inline void CSmartList::Init(CSmartList &rBase)
{
    CSmartHelperBase::CopyBase(rBase);
    m_plist = rBase.m_plist;
}

inline HRESULT CSmartList::CreateNewList(LPCWSTR pwszListName, CSmartHelperBase &rBase)
{
    CSmartHelperBase::CopyBase(rBase, pwszListName);
    return this->Create();
}

inline HRESULT CSmartList::CreateNewList(LPCWSTR pwszListName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    CSmartHelperBase::Init(pwszListName, pwszCompId, pdispContext);
    return this->Create();
}

inline HRESULT CSmartList::CreateNewList(LPCWSTR pwszListName, CMoneyInfo *pMoneyInfo)
{
    CSmartHelperBase::Init(pwszListName, pMoneyInfo);
    return this->Create();
}

inline void CSmartList::SetList(ISimpleList *plist)
{
    if (m_plist) m_plist.Release();
    m_plist = plist;
}

inline void CSmartList::Attach(ISimpleList *plist)
{
    if (m_plist) m_plist.p->Release();
    m_plist.p = plist;
}

inline ISimpleList* CSmartList::Detach()
{
    ISimpleList *plistTemp = m_plist;
    m_plist.p = NULL;
    return plistTemp;
}

inline HRESULT CSmartList::SetList(IDispatch *pdisp)
{
    HRESULT hr = S_OK;
    ISimpleList* plist = NULL;
    if (NULL!=pdisp)
    {
        if (FAILED(hr = pdisp->QueryInterface(IID_ISimpleList, (void **)&plist)))
            return hr;
    }
    this->Attach(plist);
    return hr;
}

inline HRESULT CSmartList::SetList(VARIANT *pvarlist)
{
    HRESULT hr = S_OK;
    if ((V_VT(pvarlist)&VT_TYPEMASK) != VT_DISPATCH)
        return E_NOINTERFACE;
    if (V_ISBYREF(pvarlist))
        return this->SetList(*(V_DISPATCHREF(pvarlist)));
    else
        return this->SetList(V_DISPATCH(pvarlist));
}

inline HRESULT CSmartList::GetItem(long index, BSTR *pbstrValue)
{
    _Assert(pbstrValue);
    if (!pbstrValue)
        return E_INVALIDARG;

    VARIANT var;
    HRESULT hr = S_OK;
    if (FAILED(hr = this->GetItem(index, &var, VT_BSTR)))
        return hr;
    *pbstrValue = V_BSTR(&var);
    return hr;
}

inline HRESULT CSmartList::GetItem(long index, int *pintValue)
{
    _Assert(pintValue);
    if (!pintValue)
        return E_INVALIDARG;

    VARIANT var;
    HRESULT hr = S_OK;
    if (FAILED(hr = this->GetItem(index, &var, VT_I4)))
        return hr;
    *pintValue = V_I4(&var);
    return hr;
}

inline HRESULT CSmartList::GetItem(long index, long *plongValue)
{
    _Assert(plongValue);
    if (!plongValue)
        return E_INVALIDARG;

    VARIANT var;
    HRESULT hr = S_OK;
    if (FAILED(hr = this->GetItem(index, &var, VT_I4)))
        return hr;
    *plongValue = V_I4(&var);
    return hr;

}

inline HRESULT CSmartList::GetItem(long index, short *pshortValue)
{
    _Assert(pshortValue);
    if (!pshortValue)
        return E_INVALIDARG;

    VARIANT var;
    HRESULT hr = S_OK;
    if (FAILED(hr = this->GetItem(index, &var, VT_I2)))
        return hr;
    *pshortValue = V_I2(&var);
    return hr;
}

inline HRESULT CSmartList::GetItem(long index, float *pfloatValue)
{
    _Assert(pfloatValue);
    if (!pfloatValue)
        return E_INVALIDARG;

    VARIANT var;
    HRESULT hr = S_OK;
    if (FAILED(hr = this->GetItem(index, &var, VT_R4)))
        return hr;
    *pfloatValue = V_R4(&var);
    return hr;
}

inline HRESULT CSmartList::GetItem(long index, double *pdoubleValue)
{
    _Assert(pdoubleValue);
    if (!pdoubleValue)
        return E_INVALIDARG;

    VARIANT var;
    HRESULT hr = S_OK;
    if (FAILED(hr = this->GetItem(index, &var, VT_R8)))
        return hr;
    *pdoubleValue = V_R8(&var);
    return hr;
}

inline HRESULT CSmartList::GetItem(long index, IDispatch **ppdispValue)
{
    VARIANT var[1];
    HRESULT hr = S_OK;

    _Assert(ppdispValue);
    if (!ppdispValue)
        return E_INVALIDARG;

    if (FAILED(hr = this->GetItem(index, var, VT_DISPATCH, StrictType)))
        return hr;
    *ppdispValue = V_DISPATCH(var);

    return hr;
}

inline HRESULT CSmartList::GetItem(long index, IUnknown **ppunkValue)
{
    VARIANT var[1];
    HRESULT hr = S_OK;

    _Assert(ppunkValue);
    if (!ppunkValue)
        return E_INVALIDARG;

    if (FAILED(hr = this->GetItem(index, var, VT_UNKNOWN, StrictType)))
        return hr;
    *ppunkValue = V_UNKNOWN(var);

    return hr;
}

inline HRESULT CSmartList::PutItem(long index, VARIANT *pvarValue)
{
    if (!m_plist)
        return OLE_E_BLANK;
    if (!pvarValue)
        return E_INVALIDARG;
    if (-1 == index)
        return m_plist->Add(pvarValue);
    else
        return m_plist->put_Item(index, *pvarValue);
}

inline HRESULT CSmartList::PutrefItem(long index, VARIANT *pvarValue)
{
    if (!m_plist)
        return OLE_E_BLANK;
    if (!pvarValue)
        return E_INVALIDARG;
    if (-1 == index)
        return m_plist->Add(pvarValue);
    else
        return m_plist->putref_Item(index, *pvarValue);
}

inline HRESULT CSmartList::PutItem(long index, BSTR bstrValue)
{
    _Assert(bstrValue);
    if (!bstrValue)
        return E_INVALIDARG;

    VARIANT var;
    V_VT(&var) = VT_BSTR;
    V_BSTR(&var) = bstrValue;
    return this->PutItem(index, &var);
}

inline HRESULT CSmartList::PutItem(long index, int intValue)
{
    CComVariant var(intValue);
    return this->PutItem(index, &var);
}

inline HRESULT CSmartList::PutItem(long index, long longValue)
{
    CComVariant var(longValue, VT_I4);
    return this->PutItem(index, &var);
}

inline HRESULT CSmartList::PutItem(long index, short shortValue)
{
    CComVariant var(shortValue);
    return this->PutItem(index, &var);
}

inline HRESULT CSmartList::PutItem(long index, float floatValue)
{
    CComVariant var(floatValue);
    return this->PutItem(index, &var);
}

inline HRESULT CSmartList::PutItem(long index, double doubleValue)
{
    CComVariant var(doubleValue);
    return this->PutItem(index, &var);
}

inline HRESULT CSmartList::PutItem(long index, IDispatch *pdispValue)
{
    _Assert(pdispValue);
    if (!pdispValue)
        return E_INVALIDARG;

    VARIANT var[1];
    V_VT(var) = VT_DISPATCH;
    V_DISPATCH(var) = pdispValue;
    return this->PutrefItem(index, var);
}

inline HRESULT CSmartList::PutItem(long index, IUnknown *punkValue)
{
    _Assert(punkValue);
    if (!punkValue)
        return E_INVALIDARG;

    VARIANT var[1];
    V_VT(var) = VT_UNKNOWN;
    V_UNKNOWN(var) = punkValue;
    return this->PutrefItem(index, var);
}

inline HRESULT CSmartList::PutItem(long index, IDictionary *pdictValue)
{
    _Assert(pdictValue);
    if (!pdictValue)
        return E_INVALIDARG;

    HRESULT hr = S_OK;
    CComPtr<IDispatch> pdisp;
    if (FAILED(hr = pdictValue->QueryInterface(IID_IDispatch, (void **)&pdisp)))
        return hr;
    return this->PutItem(index, pdisp);
}

inline HRESULT CSmartList::PutItem(long index, ISimpleList *plistValue)
{
    _Assert(plistValue);
    if (!plistValue)
        return E_INVALIDARG;

    HRESULT hr = S_OK;
    CComPtr<IDispatch> pdisp;
    if (FAILED(hr = plistValue->QueryInterface(IID_IDispatch, (void **)&pdisp)))
        return hr;
    return this->PutItem(index, pdisp);
}

inline HRESULT CSmartList::GetCount(long *pl)
{
    _Assert(pl);
    _Assert(m_plist);
    if (!pl)
        return E_INVALIDARG;
    if (!m_plist)
        return OLE_E_BLANK;
    return m_plist->get_Count(pl);
}

inline HRESULT CSmartList::Delete(long index)
{
    _Assert(m_plist);
    if (!m_plist)
        return OLE_E_BLANK;
    return m_plist->Delete(index);
}





//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//*****************************************************************************************
//=========================================================================================
//
//  CSmartHelperBase inlines
//
//=========================================================================================
//*****************************************************************************************
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

inline void CSmartHelperBase::Init(LPCWSTR pwszObjName, LPCWSTR pwszCompId, IDispatch *pdispContext)
{
    HRESULT hr = S_OK;

    // check arguments
    _Assert(pwszCompId);
    _Assert(pwszObjName);

    lstrcpyW(m_wszCompId, pwszCompId);
    lstrcpyW(m_wszObjName, pwszObjName);

    m_pdispContext = pdispContext;
    m_pMoneyInfo = NULL;
}

inline void CSmartHelperBase::Init(LPCWSTR pwszObjName, CMoneyInfo *pMoneyInfo)
{
    HRESULT hr = S_OK;

    // check arguments
    _Assert(pwszObjName);
    _Assert(pMoneyInfo);
    _Assert(pMoneyInfo->m_wszCompId);

    lstrcpyW(m_wszCompId, pMoneyInfo->m_wszCompId);
    lstrcpyW(m_wszObjName, pwszObjName);

    m_pdispContext = pMoneyInfo->m_pdispContext;
    m_pMoneyInfo = pMoneyInfo;
}

inline void CSmartHelperBase::CopyBase(CSmartHelperBase &rBase)
{
    _Assert(rBase.m_wszCompId);
    _Assert(rBase.m_wszObjName);

    m_pdispContext = rBase.m_pdispContext;
    lstrcpyW(m_wszCompId, rBase.m_wszCompId);
    lstrcpyW(m_wszObjName, rBase.m_wszObjName);
    m_pMoneyInfo = rBase.m_pMoneyInfo;
}

inline void CSmartHelperBase::CopyBase(CSmartHelperBase &rBase, LPCWSTR pwszObjName)
{
    _Assert(rBase.m_wszCompId);

    m_pdispContext = rBase.m_pdispContext;
    lstrcpyW(m_wszCompId, rBase.m_wszCompId);
    lstrcpyW(m_wszObjName, pwszObjName);
    m_pMoneyInfo = rBase.m_pMoneyInfo;
}

inline HRESULT CSmartHelperBase::MultiplyMoney(CURRENCY &result, const CURRENCY &cy, double mul, WCHAR *ResultKey)
{
    HRESULT hr = S_OK;
    _Assert(m_pMoneyInfo);
    CComBSTR ResultPath;
    if (ResultKey)
    {
        ResultPath = m_wszObjName;
        ResultPath += ".";
        ResultPath += ResultKey;
    }
    if (FAILED(hr = m_pMoneyInfo->MultiplyMoney(result, cy, mul, ResultPath)))
        goto CleanUp;

CleanUp:
    return hr;
}

inline HRESULT CSmartHelperBase::AddMoney(CURRENCY &result, const CURRENCY &cy1, const CURRENCY &cy2, WCHAR *ResultKey)
{
    HRESULT hr = S_OK;
    _Assert(m_pMoneyInfo);
    CComBSTR ResultPath;
    if (ResultKey)
    {
        ResultPath = m_wszObjName;
        ResultPath += ".";
        ResultPath += ResultKey;
    }
    if (FAILED(hr = m_pMoneyInfo->AddMoney(result, cy1, cy2, ResultPath)))
        goto CleanUp;

CleanUp:
    return hr;
}

inline HRESULT CSmartHelperBase::SubtractMoney(CURRENCY &result, const CURRENCY &cy1, const CURRENCY &cy2, WCHAR *ResultKey)
{
    HRESULT hr = S_OK;
    _Assert(m_pMoneyInfo);
    CComBSTR ResultPath;
    if (ResultKey)
    {
        ResultPath = m_wszObjName;
        ResultPath += ".";
        ResultPath += ResultKey;
    }
    if (FAILED(hr = m_pMoneyInfo->SubtractMoney(result, cy1, cy2, ResultPath)))
        goto CleanUp;

CleanUp:
    return hr;
}

#endif // !defined(___SMARTDICT_H__)
